package com.bndg.browser.ui

import android.content.Context
import android.graphics.BitmapFactory
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.blankj.utilcode.util.TimeUtils
import com.bndg.browser.R
import com.bndg.browser.db.History
import com.bndg.browser.util.Trace
import com.bndg.browser.app.AppAdapter

class HistoryAdapter(context: Context) : AppAdapter<History>(context) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HistoryViewHolder {
        return HistoryViewHolder()
    }

    inner class HistoryViewHolder : AppViewHolder(R.layout.item_history) {
        private val ivIcon: ImageView? by lazy { findViewById(R.id.iv_icon) }
        private val tvTitle: TextView? by lazy { findViewById(R.id.tv_title) }
        private val tvUrl: TextView? by lazy { findViewById(R.id.tv_url) }
        private val tvTime: TextView? by lazy { findViewById(R.id.tv_time) }
        override fun onBindView(position: Int) {
            val bean = getItem(position)
            tvTitle?.text = bean.title
            tvUrl?.text = bean.url
            tvTime?.text = TimeUtils.millis2String(bean.timestamp, "HH:mm")
            ivIcon?.let {
                if (bean.favicon != null) {
                    // 加载file图片
                    val decodeFile = BitmapFactory.decodeFile(bean.favicon)
                    Trace.d(">>>>", "decode: $decodeFile")
                    if (decodeFile != null) {
                        it.setImageBitmap(decodeFile)
                    }
                }
            }
        }
    }
}


